package com.csc.bankingsystem.web.controller;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csc.bankingsystem.core.dto.AccountInfoDTO;
import com.csc.bankingsystem.core.dto.UserInfoDTO;
import com.csc.bankingsystem.core.entity.AccountInfo;
import com.csc.bankingsystem.core.enumeration.AccountInfoStateEnum;
import com.csc.bankingsystem.core.service.AccountInfoService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired
	AccountInfoService accountInfoService;
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "welcome";
	}
	
	@RequestMapping(value = "/api/test1", method = RequestMethod.POST)
	@ResponseBody
	public Date test1() {
		return new Date();
	}
	
	@RequestMapping(value = "/api/test2", method = RequestMethod.POST)
	@ResponseBody
	public AccountInfoDTO test2(@RequestParam String str) {
		AccountInfoDTO dto = new AccountInfoDTO();
		dto.setFirstName(str);
		dto.setLastName(str);
		dto.setEmail1("test@gmail.com");
		dto.setState(AccountInfoStateEnum.ACTIVE);
		return dto ;
	}
	
	@RequestMapping(value = "/api/test3", method = RequestMethod.POST)
	@ResponseBody
	public List<AccountInfoDTO> test3() {
		List<AccountInfo> lstAccountInfo  = accountInfoService.findAll();
		List<AccountInfoDTO> lstResult = new ArrayList<AccountInfoDTO>();
		for (AccountInfo accountInfo : lstAccountInfo) {
			AccountInfoDTO dto = new AccountInfoDTO(accountInfo);
			lstResult.add(dto);
		}
		return lstResult ;
	}
	
	@RequestMapping(value = "/api/test4", method = RequestMethod.POST)
	@ResponseBody
	public AccountInfoDTO test4(@RequestParam long id) {
		AccountInfo accountInfo  = accountInfoService.find(id);
		AccountInfoDTO result = new AccountInfoDTO(accountInfo);
		return result ;
	}
	
	@RequestMapping(value = "/api/test5", method = RequestMethod.POST)
	@ResponseBody
	public String test5(@RequestBody UserInfoDTO dto) {
		System.out.println("loginId = " + dto.getLoginId());
		System.out.println("password = " + dto.getPassword());
		return "success" ;
	}
	
	@RequestMapping(value = "/api/test6", method = RequestMethod.POST)
	@ResponseBody
	public String test6(@RequestBody UserInfoDTO dto) {
		System.out.println("loginId = " + dto.getLoginId());
		System.out.println("password = " + dto.getPassword());
		return "";
	}
}
